// 文件入口
import Vue from 'vue'
import axios from 'axios'
import VueRouter from 'Vue-router'
import Vuex from 'vuex'
// 导入外部路由模块
import router from './router.js'
// 导入APP跟组件
import app from './app.vue'

import 'element-ui/lib/theme-chalk/index.css';
import { Button, Tooltip, Option, Tree, Loading, Popover, MessageBox, Switch, Table, Dialog, Transfer, TableColumn, Select, Menu, MenuItem, Submenu, Avatar, Form, FormItem, Input, Backtop, Message, Tabs, TabPane } from 'element-ui';
Vue.use(Option)
Vue.use(Button)
Vue.use(Tooltip)
Vue.use(Table)
Vue.use(Tree)
Vue.use(Popover)
Vue.use(Switch)
Vue.use(Dialog)
Vue.use(Transfer)
Vue.use(TableColumn)
Vue.use(Select)
Vue.use(Menu)
Vue.use(MenuItem)
Vue.use(Submenu)
Vue.use(Avatar)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Backtop)
Vue.use(Tabs)
Vue.use(TabPane)
// <router-view>
// 安装路由
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(Loading.directive);
Vue.prototype.$loading = Loading.service;
Vue.prototype.$message = Message;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$ajax = axios; // 在组件中使用  this.$ajax.get("").then(res=>{}).catch(err=>{})
axios.defaults.baseURL = 'http://127.0.0.1:8000/bms/'; // 腾讯云服务器地址，开放8000端口

axios.interceptors.request.use(config => {
    // Do something before request is sent
    let token = window.localStorage.getItem("BMS_token")
    if (token !== null) {
        config.headers.Authorization = `JWT ${token}`;
        return config;
    } else {
        return config;
    }


}, error => {
    // Do something with request error
    return Promise.reject(error);
});

//http response 拦截器
axios.interceptors.response.use(
    undefined,
    error => {
        let res = error.response;
        switch (res.status) {
            case 400:
                return Promise.reject({
                    "rcode": 400,
                    "rmsg": "错误请求"
                })
            case 401:
                return Promise.reject({
                    "rcode": 401,
                    "rmsg": "未登录"
                })
            //console.log('未登录 或者token过期');
            case 403:
                return Promise.reject({
                    "rcode": 403,
                    "rmsg": "暂无操作权限"
                })
            // alert('您没有该操作权限');
            case 500:
                return Promise.reject({
                    "rcode": 500,
                    "rmsg": "服务器错误"
                })
            // alert('服务器错误');
        }
        //return Promise.reject(error.response.data) // 返回接口返回的错误信息
    });


// 全局时间过滤器
Vue.filter('alltime', function (val) {
    if (val) {
        let d = new Date(val)
        let yy = d.getFullYear()
        let mm = (d.getMonth() + 1).toString().padStart(2, '0')
        let dd = (d.getDate()).toString().padStart(2, '0')
        let hh = (d.getHours()).toString().padStart(2, '0')
        let mms = (d.getMinutes()).toString().padStart(2, '0')
        let ss = (d.getSeconds()).toString().padStart(2, '0')
        return `${yy}-${mm}-${dd} ${hh}:${mms}:${ss}`
    } else {
        return val
    }
});



let store = new Vuex.Store({
    state: {
        base_nav: null,
        user_infos: null
    },
    mutations: {
        upt_base_nav(state, nav_list) {
            state.base_nav = nav_list
        },
        upt_user_infos(state, user) {
            state.user_infos = user
        }
    },
    getters: {
        get_base_nav(state) {
            return state.base_nav
        },
        get_user_infos(state) {
            return state.user_infos
        }
    }
})

var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store
})